"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，TPCC中执行全量备份;
Create At   : 2024-11-05
Owner       : zhongchengzhi
Description :
    1、查看集群状态 cm_ctl query -Cvipd
    2、运行tpcc业务，主节点写业务，备节点读业务;
    3、清理备份目录
    4、初始化备份目录
    5、新增备份实例
    6、执行全量备份
    7、恢复环境
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、tpcc进程运行
    3、清理备份成功
    4、初始化备份目录成功
    5、新增备份实例成功
    6、执行全量备份成功
    7、恢复环境正常
History     :
    1、2024-11-05 创建
    2、2025-07-12 改造用例为1p1s环境可执行
"""


import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread

Primary_SH = CommonSH('PrimaryDbUser')
Primary_ROOTSH = CommonSH('PrimaryRoot')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.constant = Constant()
        self.primaryRoot = Node('PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

    def test_shared_storage(self):
        text = '======step1:查看集群状态; expect:集群状态正常======'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '======step2:运行tpcc业务;expect:tpcc进程运行======'
        self.log.info(text)
        primary_tpcc = ComThread(self.com.start_tpcc,
                                 args=(self.primaryRoot, macro.TPCC_PATH,
                                       './runBenchmark.sh props_write.pg'))
        primary_tpcc.setDaemon(True)
        primary_tpcc.start()
        status = Primary_ROOTSH.exec_cmd_under_root("ps -ux | grep './runBenchmark.sh' | grep -v -- '--color'")
        self.assertIn('props_write.pg', status, '执行失败')
        time.sleep(10)

        text = '======step3:清理备份目录; expect:清理备份目录成功======'
        self.log.info(text)
        status = Primary_SH.exec_cmd_under_root(f'rm -rf {macro.DB_BACKUP_PATH}')
        self.log.info(status)
        
        text = '======step4:初始化备份目录; expect:初始化备份目录成功======'
        self.log.info(text)
        status = Primary_SH.exec_pro_backup_init(macro.DB_BACKUP_PATH, get_detail = True)
        self.log.info(status)
        self.assertIn('success', status, '执行失败' + text)
        time.sleep(2)

        text = '======step5:新增备份实例; expect:新增备份实例成功======'
        self.log.info(text)
        status = Primary_SH.exec_pro_back_add(macro.DB_BACKUP_PATH, 'pro1', "--enable-dss --instance-id 0 --vgname=\"+data,+log\"", get_detail = True)
        self.log.info(status)
        self.assertIn('inited', status, '执行失败' + text)
        time.sleep(2)

        text = '======step6:执行全量备份; expect:执行全量备份成功======'
        self.log.info(text)
        status = Primary_SH.exec_pro_backup_backup(macro.DB_BACKUP_PATH, 'pro1', 'FULL', 'postgres', get_detail = True)
        self.log.info(status)
        self.assertIn('completed', status, '执行失败' + text)
        time.sleep(10)

    def tearDown(self):
        text = '======step7:恢复环境为初始集群状态; expect:成功======'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        stop_tpcc_cmd = "ps -ef | grep runBenchmark | grep -v grep | " \
                        + "awk '{{print $2}}' | xargs kill -9"
        status = stop_tpcc = Primary_ROOTSH.exec_cmd_under_root(stop_tpcc_cmd)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn(self.constant.cm_stop_success_msg, status,
                      '执行失败' + text)

        startdn = Primary_SH.exec_cm_ctl('start')
        self.log.info(startdn)
        self.assertIn(self.constant.cm_start_success_msg, startdn,
                '拉起dn节点失败')
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.log.info(f'======{os.path.basename(__file__)} end======')


